//
//  YJDPDF.m
//  YJDSDK
//
//  Created by skyline on 2018/2/2.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDPDF.h"
#import "SK_Masonry.h"
#import "NSString+category.h"
#import <WebKit/WebKit.h>

@interface YJDPDF()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView * webView;
@end
@implementation YJDPDF
- (instancetype)initWithPDF:(NSString *)pdf
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.pdf = pdf;
        [self addTap];
    }
    return self;
}

- (WKWebView *)webView
{
    if (!_webView)
    {
        _webView = [[WKWebView alloc]init];
        _webView.scrollView.bounces = NO;
        _webView.navigationDelegate = self;
        [self addSubview:_webView];
        [_webView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return _webView;
}

- (void)setPdf:(NSString *)pdf
{
    if (_pdf != pdf)
    {
        _pdf = pdf;
        [self addRequest];
        [YJDHud yjd_startHud];
    }
    self.hidden = NO;
}

- (void)addTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForTapWkWebView)];
    [self.webView.scrollView addGestureRecognizer:tap];
}

- (void)addRequest
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"fly" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *path = [bundle pathForResource:_pdf ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)actionForTapWkWebView
{
    self.hidden = YES;
}

#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    [YJDHud yjd_startHud];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [YJDHud yjd_stopHud];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [YJDHud yjd_stopHud];
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
}

// 跳转失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
}

//拦截判断
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    [YJDHud yjd_startHud];
    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
    decisionHandler(policy);
}

//
//
//- (void)setLabelBounds
//{
//    CGRect rect = [self.text  getHeightWithWidth:SCREEN_WIDTH withFont:[UIFont systemFontOfSize:15]];
//    _label.bounds = CGRectMake(0, 0, rect.size.width, ceil(rect.size.height));
////    self.contentSize = CGSizeMake(SCREEN_WIDTH, _label.bounds.size.height + 300);
//}
//
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [super touchesBegan:touches withEvent:event];
//    NSSet *allTouches = [event allTouches];
//    UITouch *touch = [allTouches anyObject];
//    touchPoint = [touch locationInView:[touch view]];
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [super touchesEnded:touches withEvent:event];
//    NSSet *allTouches = [event allTouches];
//    UITouch *touch = [allTouches anyObject];
//    CGPoint pointEnd = [touch locationInView:[touch view]];
//    
//    float x = fabs(pointEnd.x - touchPoint.x);
//    float y = fabs(pointEnd.y - touchPoint.y);
//    if (x <=10 && y <= 10 )
//    {
//        self.hidden = YES;
//    }
//}
//
//- (NSString *)text
//{
//    return @"重要须知：\n 
//　　第一 、用户在使用本软件及相关各项服务之前，请仔细阅读本《最终用户使用许可协议》(下称《协议》)中各项条款。本公司在此特别提醒用户认真阅读本《协议》的全部条款，特别是其中免除或者限制本公司责任的条款（该等条款通常含有“不负任何责任”、“无义务”等词汇）中的条款和其它限制用户权利的条款（该等条款通常含有“不得”等词汇），这些条款应在中国法律所允许的范围内最大程度地适用。  
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
